<?php
namespace Home\Controller;
use Think\Controller;
class RegUserController extends Controller {
    public function index(){
     if(!IS_POST || !IS_AJAX ) $this->redirect('/');
     $data= array();
     $vercode=trim(I('post.VerificationCode'));
     $RUserPwd=trim(I('post.RUserPwd'));
     $data['username']=trim(I('post.UserName'));
     $data['nicename']=trim(I('post.NiceName'));
     $data['password']=trim(I('post.UserPwd'));
     $data['email']=trim(I('post.Email'));
		$parm=trim(I('post.parm'));
		if (!$vercode || !$RUserPwd || !$data['username'] || !$data['password'] || !$data['email'] || strlen($data['username']) > 12 || strlen($data['username']) < 4 || strlen($RUserPwd) > 16 || strlen($RUserPwd) < 6 || $RUserPwd <> $data['password'] || strlen($data['email']) > 30 || strlen($data['email']) < 4) {
			$data2['Msg'] = '用户名或者密码错误，请重新输入';
			$this->ajaxReturn($data2);
		}
		if(!check_verify ($vercode)){
			$data2['Msg']="验证码输入有误，请重新输入";
			$this->ajaxReturn($data2);
		}
		if(!pregE($data['email'])) {
			$data2['Msg']="邮箱输入有误，请重新输入";
			$this->ajaxReturn($data2);
		}
		if(!pregE($data['email'])) {
			$data2['Msg']="输入有误，请重新输入";
			$this->ajaxReturn($data2);
		}
	$data1['username']=array('EQ',$data['username']);
	$data1['email']=array('EQ',$data['email']);
	$data1['_logic']='or';
	$map['_complex']=$data1;
	$list = M('user')->where ($map)->select();
	if(count($list)>0){
		$data2['Msg']="名字或邮箱被占用，请重新输入";
		$this->ajaxReturn($data2);
	}
	if($_SESSION ['parm']){
		$data['tid']=$_SESSION ['parm'];
		if(C('REG_ONE_ON') && $tid=M('user')->where(array('Id'=>$parm))->find()){
			if (C('REG_TWO_ON') && $tids=M('user')->where(array('Id'=>$tid['tid']))->find()){
				$money=C('REG_TWO');
				$data2 = array(
					'tmoneryall' => array('exp', '`tmoneryall`+' . $money) ,
					'tmonery2' => array('exp', '`tmonery2' . '`+' . $money) ,
				);
				$tidss=M('user')->where(array('Id' => $tids['id']))->data($data2)->save();
			}
			$money=C('REG_ONE');
			$data2 = array(
				'tmoneryall' => array('exp', '`tmoneryall`+' . $money) ,
				'tmonery' => array('exp', '`tmonery' . '`+' . $money) ,
			);
			$tids=M('user')->where(array('Id' => $tid['id']))->data($data2)->save();
		}
		session('parm',null);
	}
      $data['password']=md5($data['password']);
      $data['regip']=get_client_ip();
      $data['regtime']=time();
      $list=M('user')->data($data)->add();
    if($list){
      $data2['Success']=true;
      $this->ajaxReturn($data2);
    }
      $data2['Msg']="发生错误，请重新注册";
      $this->ajaxReturn($data2);
    } 
}
